 package com.yangguo.code.generator.util;

 import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

 @Slf4j
 public class XiChengEncryptionUtil {

     public static String encrypt(String param, String dataKey) {
             try {
                 SecretKeySpec keySpec = new SecretKeySpec(dataKey.getBytes(), "AES");
                 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                 cipher.init(Cipher.ENCRYPT_MODE, keySpec);

                 byte[] encryptedData = cipher.doFinal(param.getBytes());

                 String base64Encrypted = Base64.getEncoder().encodeToString(encryptedData);

                 return base64Encrypted.replace("+", "*").replace("/", "-").replace("=", "");
             } catch (Exception e) {
                 log.error("加密失败", e);
             }
             return "";
         }

         public static String decrypt(String encryptedParam, String dataKey) throws Exception {
             // 还原替换的字符
             String replacedParam = encryptedParam.replace("*", "+").replace("-", "/");
             // 根据 Base64 编码的特性，补齐缺失的 '='
             while (replacedParam.length() % 4 != 0) {
                 replacedParam = replacedParam + "=";
             }

             // 进行 Base64 解码
             byte[] decodedData = Base64.getDecoder().decode(replacedParam);

             // 创建 AES 密钥规范
             SecretKeySpec keySpec = new SecretKeySpec(dataKey.getBytes(), "AES");
             // 获取 Cipher 实例，使用 AES/ECB/PKCS5Padding 模式
             Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
             // 初始化解密模式
             cipher.init(Cipher.DECRYPT_MODE, keySpec);

             // 执行解密操作
             byte[] decryptedData = cipher.doFinal(decodedData);

             // 将解密后的字节数组转换为字符串
             return new String(decryptedData);
         }

         public static void main(String[] args) throws Exception {
             // String json =
             // "{\"abbrname\":\"北京按摩医院停车场\",\"address\":\"西城区宝产胡同7号\",\"ationname\":\">无\",\"bizspace\":12,\"contact\":\"王全杰\",\"dsspace\":1,\"exspace\":0,\"fastpile\":0,\"fullname\":\"北京按摩医院停车场\",\"id\":\"c87612a9-5aef-43fa-87b6-4d5f54fc41d1\",\"isname\":\"北京静态交通投资运营有限公司\",\"issn\":\"911M2K2819HM928\",\"lat\":39.9233,\"lng\":\"116.3605\",\"muname\":\"长安商场运营商\",\"musn\":\"91110105801123721H\",\"nespace\":0,\"numentry\":2,\"numexit\":2,\"phone\":\"18310177792\",\"recordno\":\"西003G024\",\"slowpile\":0,\"smartlevel\":0,\"space\":17,\"street\":1,\"type\":0,\"updatetime\":1742872378}";
             // // 对发送的数据进行加密处理
             String dataKey = "e115dfc1a0d24d04"; // 16 字节的密钥
             //
             // String encryptedParam = encrypt(json, dataKey);
             // System.out.println("西城明文数据-加密：" + encryptedParam);
             // // 解密
             // String decryptedParam = decrypt(encryptedParam, dataKey);
             // System.out.println("西城密文数据-解密：" + decryptedParam);


             String myData =
                 "{\"data\":\"HTNEYy4f9fD1USmCLN6qFtg6BNlzsgBzmJ9Nb7nwS2VzFFGVN2cSBdEYz2jCiwQGwpXVkDDsuNi5dWPlUZaXnx6N3-FVk029GMTONiAQ3m7Dy6uUmweP35BD6QA-938ajN3ZU6TIEy-P-Nz4rqoqisUmNin90S2fSAVzQbFoSqhsK0nOH-8XcNYyzttDfQKe6koIG2RJCVxoadIqD9BDAOpI8XQH0ziegSEjypERqAlwtDT5F7jwZXknvPVngB3wU*tPndb0Z4YVHidMGgE3KLRVTrffM0NIscc2n*hLQRdfSvrKLWtbDlHqAEdU4U9QVxwsdXRPlsS4WZH*cHtdoYFbxuV5qQQp2KXF9-hdxBM6Z12MvxS02yj0*AtV81iFkvWQWAR7S7Okn4Amtv6NQNQF3Ts2pw-in35Vlj9ZFhPWHPTeheNi54XmKRtGfIxYjI2ChQjPNzJsi-IooG-XlvkEwQbo40dUpf8TDcsFUIakvYTifO2GMKsiuRBbCf6n*RfoI-CH2zs2z*ILXK1XforOyipRfgZFcLphAri-0M0xlKFYC17KkTn1IkrzxqoKgzQX50*HSe9mFLfX8Y3xag7CZOoI1mpGd9xbO2oi5cFQJB7722oNQWdSpLRHp9-wToAjtCYtTjZmj86tqjTSwx*sYdNKO4JMeixbPhMimUOiSbp7K2sCgcmdmTnbQsRRy-C2Eehc66SXZ6HRG-zl068d5NGDaRV4Fjvt9QOs5SZGHJw*7blsq24j9-i7K*4HHSk1cDjIJooOQvFGi3qc9JX3f3awZ6Xbr-VM-nfw2rXjy*vxy-oLnYkcyDcCiZpNZBKyT1grq1HgkWafviRx7Q\"}";
             JSONObject myObj = JSON.parseObject(myData);
             // 解密
             String decryptedParam = decrypt(myObj.getString("data"), dataKey);
             System.out.println("静态加密数据-解密：" + decryptedParam);

             // String a =
             // "peSxkTN31JkB-GDgYTDUjlKMGZK-ZlvZsX34p9GXlF5XuGYVRBSf2AAg*Eye8QDs2KJbag4NS423PJezHj90W75vSLnge2ZkcxcAiX-hDIknFGbl3IOdIVXUUehmUdnuh5qTJ8npOZtXKiGULUwJ2Q23qhJdri1tUKXsQ1YpETzgcJDAPWlXFUSN0B4Uj7*oYL1Mko3vL6a58ia3QURdOmSSZqh-45WpSGGgu7GNbpsItezdxqDVNS21unneO3-oQGyFeQE4DzuQAlbaMD59RdSoAQb-XpOPqtLneyLMy8fe2idxOp2oMy9qYDP2K855wYsFrmOe01XUSIpOChwIYffKxrVeVLGRHKU-uLXwGMdswbXEt1aLFWoWK-yY09PTEkJMFkOPRSY4Ciu8U-1p-DMXsqcHwbaKXB4VTWY6uVdP5ecoHNesbU6TTvkfvYafck0y7FLDdnBS*xUwXcvpQI7UHcyaXe0JzJFovKRU*q4rf5Nqj3VRYcKw6ki940uutqxxGJx0ZdNimtApX9W4jlrE-7br1DHM3Mnu5VSkxh8HF6QzFx1WxC8s*CzY8-yL5l*hPKwDJAuUhw86uqCmSrFPUFQYFE6xMs7LwDQI24GCM5qcOaiIsoB-67MVGNkx-4stFVJH*fttDuYiCcxCOTFk2nnUOBA4i2zAY0ntn0Jwui5WHBW7hPGM5JMlbby4jSyokJPYfTEVaAFkxQrjugS2BjX3eK37-fKZH-ACfVdoe42BJN7MBZkVd6mx3G2osBk*5dB7pzijXX9A9CKG2g";
             // decryptedParam = decrypt(a, dataKey);
             // System.out.println("静态加密数据2-解密：" + decryptedParam);
             //
             //
             // decryptedParam = My.decrypt(a, dataKey);
             // System.out.println("静态加密数据3-解密：" + decryptedParam);
         }

     }

